

require('../css/commCss/basic.css')
require('../css/commCss/normalize.css')
require('../css/commCss/property.css')
require('../css/commCss/reset.css')


require('../css/register.less')

const CaptchaMini = require("captcha-mini")

//引入axios
const axios = require('axios')

document.addEventListener('DOMContentLoaded', function () {


    // 获取元素
    const phnumberText = document.querySelector('.phnumber-text')//手机号
    const registerVerification = document.querySelector('.register-verification')//验证码
    const registerPassword = document.querySelector('.register-password')//密码
    const registerPasswordagain = document.querySelector('.register-passwordagain')//再次确认密码
    const registerBtn = document.querySelector('.register-btn')//注册按钮
    const errMsg = document.querySelector('.errMsg')

    let captchaCoad=''
    let captcha2 = new CaptchaMini({
        lineWidth: 1,   //线条宽度
        lineNum: 3,       //线条数量
        dotR: 2,          //点的半径
        dotNum: 25,       //点的数量
        preGroundColor: [10, 80],    //前景色区间
        backGroundColor: [150, 250], //背景色区间
        fontSize: 50,           //字体大小
        fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'],  //字体类型
        fontStyle: 'fill',      //字体绘制方法，有fill和stroke
        content: '一个验证码abcdefghijklmnopqrstuvw生成的插件使用的是canvas显示',  //验证码内容
        length: 6    //验证码长度
    }); 
    captcha2.draw(document.querySelector('#captcha'), r => {
        console.log(r);
        captchaCoad=r
    });



    //需求:点击注册按钮,验证输入信息的合法性
    registerBtn.addEventListener('click', function () {

        // console.log(123);

        //获取输入信息
        const phoneVal = phnumberText.value;//手机号
        const verificationVal = registerVerification.value;//验证码
        const passwordVal = registerPassword.value;//密码
        const pswAgainVal = registerPasswordagain.value;//再次确认密码
        // console.log(phoneVal);
        // console.log(passwordVal);
        // console.log(pswAgainVal);


        //判断
        if (!/^1[3-9][0-9]{9}$/.test(phoneVal)) {
            errMsg.textContent = '手机号不正确'
            return;

        }else if(registerVerification.value!=captchaCoad){
            errMsg.textContent = '验证码错误'
            return;

        }else if (!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)) {
            errMsg.textContent = '密码错误'
            return;

        } else if (pswAgainVal != passwordVal) {
            errMsg.textContent = '两次密码不一致'
            return;

        } else {
            //清空错误信息
            errMsg.textContent = ''

            //发送请求-注册账号
            axios.post('http://139.9.177.51:8099/users/add', {
                account: phoneVal,
                password: passwordVal
            }).then(function (res) {
                if (res.data.status === 0) {
                    //成功
                    // 先弹出一个轻提示

                    // 跳转登录页面
                    location.href = './sign in.html'
                } else {
                    //失败
                    //弹出一个轻提示
                }
            })
        }


    })
})